import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/model/food_sample/food_sample_model.dart';
import 'package:ls_work/net/response/food_sample/food_sample_list_data.dart';
import 'package:ls_work/res/gaps.dart';
import 'package:ls_work/util/ext_utils.dart';

import '../../message/message_config.dart';
import '../../ui/food_sample/add_food_sample_page.dart';

class FoodSampleListViewModel extends BaseListViewModel<FoodSampleListData> {
  FoodSampleListViewModel(BuildContext context) : super(context);

  var model = FoodSampleModel();

  @override
  Future<List<FoodSampleListData>> requestListData() async {
    return await model.getSampleList();
  }

  Widget contentItem(int index) {
    var item = listItems[index];
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            CommonText('留样编号：${item.sampleNo}',
                textSize: 17, fontWeight: FontWeight.bold),
            CommonText("数量：${item.sampleNum}")
          ],
        ),
        Gaps.vGap8,
        CommonText("留样人：${item.customerName}"),
        Gaps.vGap8,
        CommonText("留样时间：${item.sampleDate}"),
        Gaps.vGap8,
        CommonText("消毒时间：${item.sterDate}"),
        Gaps.vGap8,
        CommonText("销毁时间：${item.destroyedDate}"),
        Gaps.vGap8,
        CommonText("登记时间：${item.updatedDate}"),
        Gaps.vGap8,
        Gaps.dividerLine,
        Row(
          children: [
            CommonText('修改', textAlign: TextAlign.center).onTap(() {
              Go().push(AddFoodSamplePage(
                id: item.id.toString(),
                isChange: true,
                title: "留样修改",
              ));
            }).setWeight(1),
            CommonText('删除', textAlign: TextAlign.center, textColor: Colors.red)
            .onTap(() {
              showApplyDialog(context: context, onSubmit: (){
                deleteSample(item.id.toString());
              },message: '确定删除该条留样吗');
            })
                .setWeight(1),
          ],
        ).addToContainer(margin: EdgeInsets.only(top: 20))
      ],
    )
        .addToContainer(
            padding: EdgeInsets.symmetric(horizontal: 16, vertical: 16))
        .onTap(() {
      Go().push(AddFoodSamplePage(
        id: item.id.toString(),
        isChange: false,
        title: "留样详情",
      ));
    });
  }

  @override
  void onCreated() {
    super.onCreated();
    requestRefresh();
  }

  @override
  void receiveMessage(SendMessageEvent event) {
    super.receiveMessage(event);
    if (event.msgCode == MessageConfig.REFRESH_SAMPLE_LIST) {
      requestRefresh();
    }
  }

  Future<void> deleteSample(String id) async {
    showLoadingDialog(msg: '删除中...');
    try {
      await model.deleteSample(id);
      hideDialog();
      requestRefresh();
    } catch (e) {
      showToast(NetErrorUtils.getNetError(e));
      hideDialog();
    }
  }
}
